using System;
using System.Collections;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.ConnectionPool;
using OracleInternal.I18N;
using OracleInternal.NotificationServices;

namespace OracleInternal.ServiceObjects
{
	internal class OracleChunkNotificationManager : OracleONSNotificationManager
	{
		protected override string GetEventTypeForNotification(string eventTypeFromDb, string serviceName)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleChunkNotificationManager, OracleTraceFuncName.GetEventTypeForNotification);
			}
			try
			{
				string text = "(%\"eventType=database/event/chunk\")";
				string arg = "static";
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.SvcObj, OracleTraceClassName.OracleONSNotificationManager, OracleTraceFuncName.RegisterForNotification, $"(SHARD) (string.type={arg}) (subscription.string={text})");
				}
				return text;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleChunkNotificationManager, OracleTraceFuncName.GetEventTypeForNotification);
				}
			}
		}

		protected override void HandleEvent(object notification)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262400, OracleTraceClassName.OracleChunkNotificationManager, OracleTraceFuncName.HandleEvent);
			}
			try
			{
				if (notification == null)
				{
					return;
				}
				try
				{
					byte[] array = Array.ConvertAll(((Notification)notification).body(), (sbyte a) => (byte)a);
					string text = Conv.GetInstance(871).ConvertBytesToString(array, 0, array.Length);
					if (ProviderConfig.m_bTraceLevelPrivate)
					{
						Trace.Write(OracleTraceLevel.Private, OracleTraceTag.RLB, OracleTraceClassName.OracleChunkNotificationManager, OracleTraceFuncName.HandleEvent, text);
					}
					Hashtable hashtable = new Hashtable();
					text = text.Trim();
					string[] array2 = text.Split(' ', '\t');
					for (int i = 0; i < array2.Length; i++)
					{
						string text2 = array2[i].Trim();
						if (string.IsNullOrEmpty(text2))
						{
							continue;
						}
						int num = text2.IndexOf('=');
						string text3 = text2.Substring(0, num).ToLower();
						object obj;
						if (string.Compare(text3, "keys", ignoreCase: true) == 0 || string.Compare(text3, "splitvalue", ignoreCase: true) == 0)
						{
							obj = text2.Substring(num + 2, text2.Length - num - 3).Split(new char[1]
							{
								','
							});
						}
						else
						{
							obj = text2.Substring(num + 1, text2.Length - num - 1);
							if (string.Compare(text3, "timestamp", ignoreCase: true) == 0)
							{
								obj = obj?.ToString() + " " + array2[++i];
							}
						}
						hashtable.Add(text3, obj);
					}
					float result = 0f;
					float.TryParse(hashtable["version"]!.ToString(), out result);
					if (!((double)result >= 2.0))
					{
						string text4 = ((hashtable["event_type"] != null) ? hashtable["event_type"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text5 = ((hashtable["chunk"] != null) ? hashtable["chunk"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text6 = ((hashtable["newchunk"] != null) ? hashtable["newchunk"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text7 = ((hashtable["tochunk"] != null) ? hashtable["tochunk"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text8 = ((hashtable["instance"] != null) ? hashtable["instance"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text9 = ((hashtable["database"] != null) ? hashtable["database"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text10 = ((hashtable["db_domain"] != null) ? hashtable["db_domain"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text11 = ((hashtable["timestamp"] != null) ? hashtable["timestamp"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text12 = ((hashtable["timezone"] != null) ? hashtable["timezone"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text13 = ((hashtable["priority"] != null) ? hashtable["priority"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text14 = ((hashtable["hash"] != null) ? hashtable["hash"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string text15 = ((hashtable["status"] != null) ? hashtable["status"]!.ToString()!.ToLowerInvariant() : string.Empty);
						string[] array3 = ((hashtable["keys"] != null) ? ((string[])hashtable["keys"]) : null);
						string[] array4 = ((hashtable["splitvalue"] != null) ? ((string[])hashtable["splitvalue"]) : null);
						if (ProviderConfig.m_bTraceLevelPrivate)
						{
							Trace.Write(OracleTraceLevel.Private, OracleTraceTag.HA, OracleTraceClassName.OracleChunkNotificationManager, OracleTraceFuncName.HandleEvent, "Event=" + text4 + ";Chunk=" + text5 + ";newchunk=" + text6 + ";tochunk=" + text7 + ";Instance=" + text8 + ";Database=" + text9 + ";Database domain=" + text10 + ";Status=" + text15 + ";TimeStamp=" + text11 + ";TimeZone=" + text12 + ";Priority=" + text13 + ";Hash=" + text14 + ";Keys=" + StringArrayToString(array3) + ";Splitvalue=" + StringArrayToString(array4) + "\n");
						}
						OracleConnectionDispenser<OraclePoolManager, OraclePool, OracleConnectionImpl>.ProcessChunkMoveEvent(new OracleChunkEventArgs(text4, text5, text8, text9, text10, text15, text6, text7, text14, text13, array3, array4, text11, text12));
					}
				}
				catch (Exception ex)
				{
					if (ProviderConfig.m_bTraceLevelPrivate)
					{
						Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)268697600, OracleTraceClassName.OracleChunkNotificationManager, OracleTraceFuncName.HandleEvent, "OracleShardChunkMoveNotificationManager::HandleEvent() failed. - " + ex.Message);
					}
				}
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)262656, OracleTraceClassName.OracleChunkNotificationManager, OracleTraceFuncName.HandleEvent);
				}
			}
		}

		private static string StringArrayToString(string[] strArray)
		{
			string result = string.Empty;
			if (strArray != null && strArray.Length != 0)
			{
				result = "[";
				for (int i = 0; i < strArray.Length; i++)
				{
					result += strArray[i];
					if (i != strArray.Length - 1)
					{
						result += ",";
					}
				}
				result += "]";
			}
			return result;
		}
	}
}
